package com.dajie.pt.controller.invitation;

import com.dajie.common.framework.spring.controller.BaseController;
import com.dajie.pt.util.GetUrlDao;
import com.dajie.pt.util.RedirectUrl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * Created by zhihaosong on 22/07/15.
 */
@Controller
@RequestMapping("redirect")
public class RedirectController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(RedirectController.class);
    private static Map<String, RedirectUrl> redirectUrlMap = null;

    @RequestMapping("/toUrl")
    @ResponseBody
    public ModelAndView redirectToUrl(@RequestParam(value = "id", required = true) String id, HttpServletRequest request, HttpServletResponse response) {
        logger.info("redirectToUrl --> id:{}", id);
        RedirectUrl urlMap = redirectUrlMap.get(id);
        if (urlMap != null) {
            logger.info("redirectToUrl --> urlMap:{}", urlMap.toString());
            String userAgent = request.getHeader("User-Agent");
            logger.info("redirectToUrl --> userAgent:{}", userAgent);
            if (userAgent.contains("Android") || userAgent.contains("iPhone") || userAgent.contains("iPad")) {
                String mobile_url = urlMap.getMobileUrl();
                try {
                    if (mobile_url != null) {
                        logger.info("redirectToUrl --> id:{} type:mobile url:{}", id, mobile_url);
                        response.sendRedirect(mobile_url);
                        return null;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    logger.info("redirectToUrl_mobile_url --> Exception:{}", e.getMessage());
                }
            } else {
                String pc_url = urlMap.getPcUrl();
                try {
                    if (pc_url != null) {
                        logger.info("redirectToUrl --> id:{} type:pc url:{}", id, pc_url);
                        response.sendRedirect(pc_url);
                        return null;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    logger.info("redirectToUrl_pc_url --> Exception:{}", e.getMessage());

                }
            }
        } else {
            try {
                response.sendRedirect("http://www.dajie.com");
            } catch (IOException e) {
                e.printStackTrace();
            }
            logger.info("redirectToUrl --> id:{} is not found", id);
        }

        return null;
    }

    @RequestMapping("/loadUrl")
    @ResponseBody
    public ModelAndView loadUrl(@RequestParam(value = "code", required = true) String code, HttpServletRequest request, HttpServletResponse response) {
        if (code.equals("dajie-pt")) {
            redirectUrlMap = GetUrlDao.getUrl();
            logger.info("redirectToUrl --> urlMap size:{}", redirectUrlMap.size());
            try {
                response.getWriter().write("get url size:" + redirectUrlMap.size());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}
